<template>
  <div>
    <el-dialog v-model="props.dialogVisible" title="选择商品" @close="cannel" width="1080">
      <el-form ref="elFormRef" :model="formData" size="default" >
        <el-row :gutter="15">
            <el-col :span="8">
              <el-form-item label="价格" prop="productName" labelWidth="60">
                <el-input-number v-model="formData.priceMin" :controls="false" :min="0" :step="0.1" :precision="2" placeholder="最低" clearable :style="{ width: '40%' }">
                </el-input-number>
                <span style="margin: 0 10px;"> - </span>
                <el-input-number v-model="formData.priceMax" :controls="false" :min="0" :step="0.1" :precision="2" placeholder="最高" clearable :style="{ width: '40%' }">
                </el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="" prop="productName">
                <el-input v-model="formData.productName" placeholder="请输入商品名称" clearable :style="{ width: '100%' }">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="flex_one_box tr">
                <el-button type="primary" size="default" @click="handleQuery">搜索</el-button>
                <el-button size="default" @click="resetQuery">重置</el-button>
              </div>
            </el-col>
          <el-col :span="24">
            <el-table
              ref="singleTableRef"
              :data="allClassList"
              stripe style="width: 100%"
              v-loading="tableLoading"
              max-height="400px"
              :highlight-current-row="!props.isCheckbox"
              @current-change="handleCurrentChange"
              @selection-change="handleSelectionChange"
              row-key="id"
            >
              <el-table-column type="skuName" label="SKU名称" width="260">
                <template #default="{row}">
                  {{ row.skuName}}
                </template>
              </el-table-column>
              <el-table-column type="originalPrice" label="销售价格" width="180">
                <template #default="{row}">
                  {{ row.originalPrice}}
                </template>
              </el-table-column>
              <el-table-column type="productName" label="秒杀价格">
                <template #default="{row}">
                  <el-input-number v-model="row.price" :controls="false" :min="0" :step="0.1" :precision="2" placeholder="秒杀价格">
                  </el-input-number>
                </template>
              </el-table-column>
              <el-table-column type="status" label="商品状态" width="100">
                <template #default="{row}">
                  <div>
                    <span class="status-circle" :class="{
                      greenStatus:row.prodStatus!=1,
                      yellowStatus:row.prodStatus==1,
                      }"></span>
                    <span>{{ row.prodStatus==1?'待上架':'已上架' }}</span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column type="status" label="SKU状态" width="100">
                <template #default="{row}">
                  <div>
                    <span class="status-circle" :class="{
                      greenStatus:row.skuStatus==1,
                      yellowStatus:row.skuStatus!=1,
                      }"></span>
                    <span>{{ row.skuStatus==1?'在售':'禁售' }}</span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column type="productName" label="当前库存" width="80">
                <template #default="{row}">
                  {{row.stock}}
                </template>
              </el-table-column>

              <el-table-column label="操作" width="80">
                <template #default="{row}">
                  <el-button type="primary" link @click="selectItem(row)" >选择</el-button>
                </template>
              </el-table-column>
             
            </el-table>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" style="padding-top: 15px;text-align: right;">
        <el-button @click="cannel" size="default">取消</el-button>
        <el-button type="primary" size="default" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch,nextTick } from 'vue'
import { getGoodsList } from "@/api/goods";
import { seckillAddList } from "@/api/activeMgmt";
import { number } from 'echarts';

const { proxy } = getCurrentInstance()

// 接收props
const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false
  },
  activeId:{
    type: Number,
    default: null,
  },
  onOk: {
    type: Function,
    default: () => {}
  },
  idsList: {
    type: Array,
    default: []
  },
  onClose: {
    type: Function,
    default: () => {}
  },
  isCheckbox:{
    type: Boolean,
    default: false
  },
  type:{
    type: String,
    default:'1',// 1 首页专题选择商品  2 限时秒杀   3 积分商城    根据类型不同 列表字段展示不同
  }
})
const idsList = ref([])
const singleTableRef = ref()
// 定义emits
const emits = defineEmits(['update:dialogVisible', 'onOk', 'onClose','idsList'])
console.log(1234,props)
// 数据状态
const elFormRef = ref(null)

const formData = ref({
  productName: undefined,
  priceMax:undefined,
  priceMin:undefined,
  relationType:3,// 关联类型，2-积分商品，3-限时秒杀，4-试用中心，5-黑卡尝鲜
})
const tableLoading = ref(false)
const rules = reactive({})

// 查询参数
const allClassList = ref([])
// 获取所有商品分类
const getAllList = () => {
  tableLoading.value=true
  const data  = {
    ...formData.value,
    current: 1,
    size: 10,
  }
  seckillAddList(data)
  .then(res => {
    let arr = res.data.records
    let selectArr = []
    tableLoading.value=false
    console.log(32323,props.idsList)
    if(props.isCheckbox){
      arr.map(item=>{
        item.sortNum = 0
        props.idsList.forEach(item2=>{
          if(item.id==item2.prodId){
            item.sortNum = item2.sortNum
            selectArr.push(item)
          }
        })
      })
    }else{
      let currentItem = null
      arr.forEach(item=>{
        props.idsList.forEach(item2=>{
          if(item.id==item2.id){
            currentItem = item
          }
        })
      })
      if(currentItem){
        singleTableRef.value?.setCurrentRow(currentItem)
      }
      
    }
    allClassList.value = arr
    nextTick(()=>{
      if(props.isCheckbox){
        singleTableRef.value?.clearSelection()
        selectArr.forEach(item=>{
          console.log(1122345,item,singleTableRef.value)
          singleTableRef.value.toggleRowSelection(item,true)
        })
      }
    })
    
  })
  .finally(() => {
    tableLoading.value=false
  })
}
// 生命周期钩子
onMounted(() => {
  console.log(112, props.dialogVisible)
})
watch(() => props.dialogVisible, value => {
  getAllList()
})

const change = (val,val2)=>{
  let list = []
  val.forEach(item=>{
    if(item.length){
      list.push(item[item.length-1])
    }
  })
  console.log(1234,list)
  idsList.value = list
}
// 方法
const cannel = () => {
  emits('onClose', false)
}

const handleConfirm = () => {
  console.log(11,idsList)
  if(idsList.value.length==0){
    proxy.$modal.msgError("请选择商品")
    return
  }
  emits('onOk', idsList)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getAllList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  formData.value = {
    productName:''
  }
  handleQuery()
}
const resetForm = (formName) => {
  // 实际项目中实现
}
function handleSelectionChange(val) {
  console.log(1233,val)
  if(props.isCheckbox){
    idsList.value = val
  }
  // emit('selectionChange', val)
}
function handleCurrentChange(val){
  if(!props.isCheckbox){
    idsList.value = [val]
  }
}
</script>

<style lang="scss">
.flex_one_box{
  height: 36px;
  display: flex;
}
</style>